﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Player;

namespace UI
{
    public class UIShop : MonoBehaviour
    {
        public static UIShop Ins { get; private set; }

        private Player.Shop _shop => Player.MyPlayer.Ins.MyShop;

        private bool _isOpen = false;
        public bool IsOpen
        {
            get => _isOpen;
            set
            {
                _isOpen = value;

                gameObject.SetActive(_isOpen);

                if (IsOpen)
                {
                    RefreshUI();
                }
            }
        }

        private Transform _tfItemRoot, _tfItemPrefab;
        private List<(Button btn, Text text)> listShopItem = new List<(Button btn, Text text)>();
        private System.Text.StringBuilder _sb;


        private void Awake()
        {
            Ins = this;

            //退出按钮
            transform.Find("Panel/ExitButton").GetComponent<Button>().onClick.AddListener(() => IsOpen = false);
            _tfItemRoot = transform.Find("Panel/Scroll View/Viewport/Content");
            _tfItemPrefab = _tfItemRoot.Find("Prefab");
            _tfItemPrefab.gameObject.SetActive(false);

            _sb = new System.Text.StringBuilder();

            IsOpen = false;
        }

        private void RefreshUI()
        {
            var arrShopItemData = _shop.ShopItemData;
            for (int i = 0; i < arrShopItemData.Length; i++)
            {
                STShopItemData shopItemData = arrShopItemData[i];

                if (listShopItem.Count <= i)
                {
                    //创建对象
                    GameObject obj = Instantiate(_tfItemPrefab.gameObject);
                    obj.transform.SetParent(_tfItemRoot);
                    listShopItem.Add((obj.GetComponentInChildren<Button>(), obj.GetComponentInChildren<Text>()));
                    int index = i;
                    listShopItem[i].btn.onClick.AddListener(() => OnBuyItemClick(index));
                }
                listShopItem[i].btn.gameObject.SetActive(true);
                listShopItem[i].btn.interactable = !shopItemData.bSold;

                _sb.Clear();
                switch (shopItemData.emItemType)
                {
                    case EMShopItemType.Item:
                        var itemData = Item.ItemDataMgr.Ins[shopItemData.strItemTag];
                        _sb.Append(itemData?.strItemName);
                        _sb.Append("\n*");
                        _sb.Append(shopItemData.nCount);
                        break;
                    case EMShopItemType.Science:
                        _sb.Append("Science\n*");
                        _sb.Append(shopItemData.nCount);
                        break;
                    case EMShopItemType.Buff:
                        break;
                    default:
                        break;
                }

                _sb.Append("\n￥");
                _sb.Append(shopItemData.nPrice);
                listShopItem[i].text.text = _sb.ToString();
            }

            for (int i = arrShopItemData.Length; i < listShopItem.Count; i++)
            {
                listShopItem[i].btn.gameObject.SetActive(true);
            }
        }

        private void OnBuyItemClick(int index)
        {
            if (_shop.NeedRefresh)
            {
                RefreshUI();
                return;
            }

            if (_shop.BuyItem(index))
                RefreshUI();
            else
                Debug.Log("购买失败");
        }

    }
}